package com.le.tester.imooc.oldfairy;

import java.util.concurrent.locks.ReentrantLock;

/**
 * createTime：2021/8/26 15:52
 * description：两者相互鞠躬,并不会释放锁
 */
public class DeadLock {
    static class Friend {
        private final String name;
        private final ReentrantLock lock = new ReentrantLock();

        public Friend(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void bow(Friend bower) {
            lock.lock();
            System.out.format("%s:%s 向我鞠躬！%n", this.name, bower.getName());
            lock.unlock();
            bower.release(this);
        }

        public void release(Friend bower) {
            lock.lock();
            System.out.format("%s,%s" + " 回了我！%n", this.name, bower.getName());
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        final Friend friend1 = new Friend("小明");
        final Friend friend2 = new Friend("小红");

        new Thread(() -> friend1.bow(friend2)).start();
        new Thread(() -> friend2.bow(friend1)).start();
    }
}
